<?php
declare (strict_types=1);

namespace app\common\model;

class Config extends Base
{

    protected $name = 'config';


    public function getTypeRowAttr($v, $d)
    {
        return $d['type'];
    }

    public function getGroupAttr($value)
    {
        return get_config_group($value);
    }

    public static function _saveGroup($param)
    {
        try {
            $msg = '';
            foreach ($param as $name => $value) {
                $info = parent::where('name', $name)->field('title,type,value,format,suffix')->find();
                if (empty($info)) continue;
                if ($info->getData('type') == 6) {
                    if ($info->value != $value) {
                        $msg .= '修改【' . $info->title . '】由<a target="_blank" href="' . $info['value'] . '">
                        <img style="width: 20px" src="' . $info['value'] . '"/></a>修改为<a target="_blank" href="' . $value . '">
                        <img style="width: 20px" src="' . $value . '"/></a>，';
                    }
                } else {
                    if ($info->value != $value) {
                        $msg .= '修改【' . $info->title . '】由【' . $info['value'] . '】修改为【' . $value . '】，';
                    }
                }
                parent::update(['value' => $value], ['name' => $name]);
            }
            if ($msg) {
                Logs::adds($msg, request()->admin->id, request()->admin->admin_name);
            }
            cache('system', null);
            return ['code' => 1, 'msg' => '保存成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


}
